﻿CREATE PROCEDURE [dbo].[usp_select_Fault_Count]
(
	@FaultID			varchar(50)		 = NULL
   ,@Application		varchar(50)      = NULL   
   ,@StartDate			varchar(25)      = '-1'  
   ,@EndDate			varchar(25)      = '-1'  
   ,@FaultCode			varchar(50)      = NULL   
   ,@FailureCategory	varchar(50)      = NULL
   ,@ErrorType			varchar(50)      = NULL 
   ,@MaxFaultSeverity	int			     = 4
   ,@StartRowIndex		int
   ,@MaximumRows		int
   ,@SortColumn			varchar(50)		 = 'DateTime'
   ,@Debug              bit              = 0
)
AS

SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10)
DECLARE @ErrorCode         int

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: '


-- declare the stored procedure parameters
/*
DECLARE @FaultID		   varchar(50);
DECLARE @Application       varchar(50);
DECLARE @StartDate         datetime;
DECLARE @EndDate           datetime;
DECLARE @FaultCode         varchar(50);
DECLARE @FailureCategory   varchar(50);
DECLARE @ExceptionType     varchar(50);
DECLARE @MinFaultSeverity  int;
DECLARE @MaxFaultSeverity  int;

DECLARE @Debug             bit;
-- now set them
SELECT
	@FaultID			   = NULL
   ,@Application           = NULL    	
   ,@StartDate             = NULL    	
   ,@EndDate               = NULL    
   ,@FaultCode             = NULL       
   ,@FailureCategory       = NULL
   ,@ExceptionType         = NULL 
   ,@MinFaultSeverity      = NULL
   ,@MaxFaultSeverity      = NULL
   ,@Debug                 = 1;
*/

-- Print parameters
IF (@Debug > 0)
BEGIN
   PRINT @DebugPrefix + '@FaultID = ' + @FaultID
   PRINT @DebugPrefix + '@Application = ' + @Application
   PRINT @DebugPrefix + '@StartDate = ' + CAST(@StartDate AS varchar)
   PRINT @DebugPrefix + '@EndDate = ' + CAST(@EndDate AS varchar)
   PRINT @DebugPrefix + '@FaultCode = ' + @FaultCode
   PRINT @DebugPrefix + '@FailureCategory = ' + @FailureCategory
   PRINT @DebugPrefix + '@ErrorType = ' + @ErrorType
   PRINT @DebugPrefix + '@MaxFaultSeverity = ' + CAST(@MaxFaultSeverity AS varchar)
END;

---------------------------------------------------------------
--	Select the total row count.
---------------------------------------------------------------

SELECT
	COUNT(FaultID)

FROM dbo.Fault f 

WHERE 
		 f.[FaultID] = ISNULL(@FaultID, f.FaultID)
   AND   f.[Application] = ISNULL(@Application, f.[Application])
   AND   f.[FailureCategory] = ISNULL(@FailureCategory, f.FailureCategory)
   AND   f.ErrorType = ISNULL(@ErrorType, f.ErrorType)
   AND   f.[FaultCode] = ISNULL(@FaultCode, f.[FaultCode])
   AND   (
			f.[DateTime] >= CASE WHEN @StartDate = '-1' THEN f.[DateTime] ELSE @StartDate END 
			AND f.[DateTime] <= CASE WHEN @EndDate = '-1' THEN f.[DateTime] ELSE @EndDate END
		)
   AND   f.[FaultSeverity] between 0 AND @MaxFaultSeverity

-- CHECK FOR ERROR
SET @ErrorCode = @@Error;
IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;
			
RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
